package com.example.democonfigserver.modules.config;

import com.example.democonfigserver.modules.app.App;
import com.example.democonfigserver.modules.env.Env;
import com.example.democonfigserver.modules.property.Property;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 配置
 *
 * @author wangbin
 */
@Entity
@Table(name = "system_configuration")
@Getter
@Setter
public class Config {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotBlank(message = "\"配置名\" 不能为空")
    private String name;

    @OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL)
    @JoinColumn(name = "configuration_id")
    @OrderBy("id asc")
    @JsonDeserialize(as = LinkedHashSet.class)
    private Set<@Valid Property> propertyList = new LinkedHashSet<>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "environment_id")
    @JsonIgnoreProperties({"config", "configList"})
    private Env env;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "configList")
    @OrderBy("id asc")
    @JsonDeserialize(as = LinkedHashSet.class)
    @JsonIgnoreProperties(value = {"config", "configList"}, allowSetters = true)
    private Set<App> appList = new LinkedHashSet<>();

}
